#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"


u8 nt38360_bufferR[50];
u8 nt38360_bufferG[50];
u8 nt38360_bufferB[50];

u8 nt38360_table_c[25]={0,1,3,5,7,11,15,23,31,39,47,55,63,79,95,111,127,143,159,175,207,223,239,247,255 };
u8 nt38360_table_s[25] ={ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58 };

void NT38360_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ??????  :ok  
}
void NT38360_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=0x34;	
		buffer[1]=0x02;
    //SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		SSD2828_W_Reg(SigMode,channel,0xB7,0x034b);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ??? ?????  :ok 
}

void NT38360_Write_Auto_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,k=0,temp;
	  u8 m = 0x00;

		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
	
		//SSD2828_W_Reg(SigMode,channel,0xB7,0740);	
		delay_ms(5);	
	
		buffer1[0] = 0x02; 
		buffer1[1] = 0xFF;
		buffer1[2] = 0xF0; //Page21
		SSD2828_W_Array(SigMode,channel,buffer1,0);

		buffer1[0] = 0x02; 
		buffer1[1] = 0xF9;
		buffer1[2] = 0x01; //
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		
		buffer1[0] = 0x02; 
		buffer1[1] = 0x50;
		buffer1[2] = 0x04; //
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		
	  buffer1[0] = 0x02; 
		buffer1[1] = 0xFF;
		buffer1[2] = 0x21; //Page21
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		
		buffer1[0] = 0x02; 
		buffer1[1] = 0xF9;
		buffer1[2] = 0x01; //
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		
		buffer1[0] = 0x02; 
		buffer1[1] = 0x33;
		buffer1[2] = 0x03; //
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
	
		buffer1[0] = 0x02; 
		buffer1[1] = 0x2F;
		buffer1[2] = nt38360_table_s[buffer[5]]; //node index
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		
		
		buffer1[0] = 0x03; 
		buffer1[1] = 0x30; //Adjust Gamma node Via
		buffer1[2] = buffer[6]; //Code RH
		buffer1[3] = buffer[7]; //Code RL
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer1[0] = 0x03; 
		buffer1[1] = 0x31; //Adjust Gamma node Via
		buffer1[2] = buffer[8]; //Code RH
		buffer1[3] = buffer[9]; //Code RL
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer1[0] = 0x03; 
		buffer1[1] = 0x32; //Adjust Gamma node Via
		buffer1[2] = buffer[10]; //Code RH
		buffer1[3] = buffer[11]; //Code RL
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5); 
	 
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
		delay_ms(2); 
    buffer[0]=0x34;	//NT38360
    buffer[1]=0x05;		
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //?? ??????  :ok  
}




void NT38360_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[17];
    u8 i,j,k=0,temp;
	  u8 m = 0x00;


		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		delay_ms(5);	
		
	  buffer1[0] = 0x02; 
		buffer1[1] = 0xFF;
		buffer1[2] = 0x21; //Page21
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(1);	
	
		buffer1[0] = 0x02; 
		buffer1[1] = 0x2F;
		buffer1[2] = nt38360_table_s[buffer[5]]; //node index
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(2);	
	
		if((buffer[3])==0x00)
		{	
			buffer1[0] = 0x03; 
			buffer1[1] = 0x30; //Adjust Gamma node Via
			buffer1[2] = buffer[6+buffer[5]*2]; //Code RH
			buffer1[3] = buffer[6+buffer[5]*2+1]; //Code RL
			SSD2828_W_Array(SigMode,channel,buffer1,0);
		}
		else if((buffer[3])==0x01)
		{		
			buffer1[0] = 0x03; 
			buffer1[1] = 0x31; //Adjust Gamma node Via
			buffer1[2] = buffer[6+buffer[5]*2]; //Code RH
			buffer1[3] = buffer[6+buffer[5]*2+1]; //Code RL
			SSD2828_W_Array(SigMode,channel,buffer1,0);
		}
		else if((buffer[3])==0x02)
		{
			buffer1[0] = 0x03; 
			buffer1[1] = 0x32; //Adjust Gamma node Via
			buffer1[2] = buffer[6+buffer[5]*2]; //Code RH
			buffer1[3] = buffer[6+buffer[5]*2+1]; //Code RL
			SSD2828_W_Array(SigMode,channel,buffer1,0);
		}
		delay_ms(5); 
		
		buffer1[0] = 0x02; 
		buffer1[1] = 0xB1;
		buffer1[2] = buffer[4]; //band select
		SSD2828_W_Array(SigMode,channel,buffer1,0);			
		
		if(buffer[3]==0x00)
		{	
				buffer1[0]=0x11;
				buffer1[1]=0xB2;		//GAMMA RED 0xB2
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	

				buffer1[0]=0x11;
				buffer1[1]=0xB3;		//GAMMA RED 0xB3
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i+16];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);					
				
				buffer1[0]=0x11;
				buffer1[1]=0xB4;		//GAMMA RED 0xB4
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i+32];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	

				
				buffer1[0]=0x03;
				buffer1[1]=0xB5;		//GAMMA RED 0xB5
				for(i=0; i<2; i++)
				{
					buffer1[2+i]=buffer[6+i+48];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
		}		
//---------------------------------------------------------------------		
		else if(buffer[3]==0x01)
		{			
				buffer1[0]=0x11;
				buffer1[1]=0xB6;		//GAMMA RED 0xB6
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	

				buffer1[0]=0x11;
				buffer1[1]=0xB7;		//GAMMA RED 0xB7
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i+16];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);					
				
				buffer1[0]=0x11;
				buffer1[1]=0xB8;		//GAMMA RED 0xB8
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i+32];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	

				
				buffer1[0]=0x03;
				buffer1[1]=0xB9;		//GAMMA RED 0xB9
				for(i=0; i<2; i++)
				{
					buffer1[2+i]=buffer[6+i+48];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
				
		}
//----------------------------------------------------------------------	
		else if(buffer[3]==0x02)
		{		
				buffer1[0]=0x11;
				buffer1[1]=0xBA;		//GAMMA RED 0xBA
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	

				buffer1[0]=0x11;
				buffer1[1]=0xBB;		//GAMMA RED 0xBB
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i+16];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);					
				
				buffer1[0]=0x11;
				buffer1[1]=0xBC;		//GAMMA RED 0xBC
				for(i=0; i<16; i++)
				{
					buffer1[2+i]=buffer[6+i+32];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	

				
				buffer1[0]=0x03;
				buffer1[1]=0xBD;		//GAMMA RED 0xBD
				for(i=0; i<2; i++)
				{
					buffer1[2+i]=buffer[6+i+48];	
				}				 
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
		}
		delay_ms(5); 	
	//---------------------------------------------------------		
		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
		delay_ms(2); 
    buffer[0]=0x34;	//NT38360
    buffer[1]=0x03;		
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //?? ??????  :ok  
}



void NT38360_Gamma_Update(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,k=0,temp;
	  u8 m = 0x00;
		u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};	
		
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
		delay_ms(5);			
		SSD2828_W_Array(SigMode,channel,PAGE2,0);	
		delay_ms(2);	
	  buffer1[0] = 0x02; 
		buffer1[1] = 0xBF;
		buffer1[2] = buffer[4]; //gamma_group_sel
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCC;
    buffer1[2] = 0x30;
    SSD2828_W_Array(SigMode,channel,buffer1,0);	
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(2);				
	
		SSD2828_W_Reg(SigMode,channel,0xBC,25); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB0); //GAMMA RED GROUP1
		for(i=0; i<24; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferR[i]);	
		}				 
		
		SSD2828_W_Reg(SigMode,channel,0xBC,25); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB1); //GAMMA RED GROUP2
		for(i=0; i<24; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferR[i+24]);	
		}				
		
		SSD2828_W_Reg(SigMode,channel,0xBC,23); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB2); //GAMMA RED GROUP3
		for(i=0; i<22; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferR[i+48]);	
		}		
		delay_ms(2); 		
//---------------------------------------------------------------------		
		SSD2828_W_Reg(SigMode,channel,0xBC,25); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB3); //GAMMA RED GROUP1
		for(i=0; i<24; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferG[i]);	
		}				 
		
		SSD2828_W_Reg(SigMode,channel,0xBC,25); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB4); //GAMMA RED GROUP2
		for(i=0; i<24; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferG[i+24]);	
		}									 
		SSD2828_W_Reg(SigMode,channel,0xBC,23); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB5); //GAMMA RED GROUP3
		for(i=0; i<22; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferG[i+48]);	
		}
		delay_ms(2); 		
//----------------------------------------------------------------------		
		SSD2828_W_Reg(SigMode,channel,0xBC,25); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB6); //GAMMA RED GROUP1
		for(i=0; i<24; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferB[i]);	
		}				 	
		SSD2828_W_Reg(SigMode,channel,0xBC,25); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB7); //GAMMA RED GROUP2
		for(i=0; i<24; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferB[i+24]);	
		}									 
		SSD2828_W_Reg(SigMode,channel,0xBC,23); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB8); //GAMMA RED GROUP3
		for(i=0; i<22; i++)
		{
			SSD2828_W_Data(SigMode,channel,nt38360_bufferB[i+48]);	
		}
		delay_ms(2); 	
	//---------------------------------------------------------			
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x01;
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(10); 		
		
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCC;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(2); 	
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);				
		delay_ms(2); 		
		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
		delay_ms(2); 
    buffer[0]=0x4E;	//NT38360
    buffer[1]=0x03;		
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //?? ??????  :ok   
}

void NT38360_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
			u8 buffer1[7];
			u16 tmp;
			u8 i,j;
			u8 PAGE2[2]={0xff,0x21};		
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	    delay_ms(15);	
			SSD2828_W_Array(SigMode,channel,PAGE2,0); 
			delay_ms(5);	    
	
			buffer1[0] = 0x02;  	
			buffer1[1] = 0xB1;
			buffer1[2] = buffer[4]; //gamma_group_sel
			SSD2828_W_Array(SigMode,channel,buffer1,0);		        
	    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
					    
	    delay_ms(10);			 
			if(buffer[3]==0x00)
			{
				Generate_MIPI_Read(pdev,SigMode,channel,0xB2 ,16, buffer+4);delay_ms(5);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB3 ,16, buffer+4+16);delay_ms(5);			
				Generate_MIPI_Read(pdev,SigMode,channel,0xB4 ,16, buffer+4+32);delay_ms(5);			
				Generate_MIPI_Read(pdev,SigMode,channel,0xB5 ,2,  buffer+4+48);delay_ms(5);		
			}
			else if(buffer[3]==0x01)
			{
				Generate_MIPI_Read(pdev,SigMode,channel,0xB6 ,16, buffer+4);delay_ms(5);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB7 ,16, buffer+4+16);delay_ms(5);			
				Generate_MIPI_Read(pdev,SigMode,channel,0xB8 ,16, buffer+4+32);delay_ms(5);			
				Generate_MIPI_Read(pdev,SigMode,channel,0xB9 ,2,  buffer+4+48);delay_ms(5);		
			}
			else if(buffer[3]==0x02)
			{
				Generate_MIPI_Read(pdev,SigMode,channel,0xBA ,16, buffer+4);delay_ms(5);
				Generate_MIPI_Read(pdev,SigMode,channel,0xBB ,16, buffer+4+16);delay_ms(5);			
				Generate_MIPI_Read(pdev,SigMode,channel,0xBC ,16, buffer+4+32);delay_ms(5);			
				Generate_MIPI_Read(pdev,SigMode,channel,0xBD ,2,  buffer+4+48);delay_ms(5);		
			}			
			
    buffer[0]=0x34;	//NT38360
    buffer[1]=0x04;
		
		delay_ms(15);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //?? ??? Gamma??  :ok  		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void NT38360_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];
	  u8 mtp_flag;
		u8 PAGE1[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x01};	
		
		u8 OTP_en_off[8]={0x07,0xED,0x00,0x00,0x00,0x00,0x00,0x00};
		u8 OTP_en_PAGE0[8]={0x07,0xED,0x7F,0x00,0x00,0x00,0x00,0x00};
	  u8 OTP_en_PAGE1[8]={0x07,0xED,0x80,0x0F,0x00,0x00,0x00,0x00};
	  u8 OTP_en_PAGE2[9]={0x08,0xED,0x00,0xE0,0x00,0x00,0x00,0x00,0x08};		
	  u8 OTP_en_PAGE3[8]={0x07,0xED,0x00,0x00,0x07,0x00,0x00,0x00};	
	  u8 OTP_en_PAGE4[8]={0x07,0xED,0x00,0x00,0x18,0x00,0x00,0x00};	
	  u8 OTP_en_PAGE5[8]={0x07,0xED,0x00,0x00,0x60,0x00,0x00,0x00};
	  u8 OTP_en_PAGE6[8]={0x07,0xED,0x00,0x00,0x80,0x00,0x00,0x00};		
	  u8 OTP_en_PAGE7[8]={0x07,0xED,0x00,0x00,0x00,0x03,0x00,0x00};		
	  u8 OTP_en_PAGE8[8]={0x07,0xED,0x00,0x00,0x00,0x0C,0x00,0x00};			
		
		u8 OTP_all_PAGE[9]={0x08,0xED,0xFF,0xEF,0xFF,0x0F,0x08,0x00,0x08};
			
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15); 
    buffer1[0] = 0x01;
    buffer1[1] = 0x28;                    
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
		buffer1[0] = 0x01;
    buffer1[1] = 0x11;  	
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(120); 	
	  SSD2828_W_Array(SigMode,channel,PAGE1,0);//Enable CMD2 Page 1
		delay_ms(5); 
	  buffer1[0] = 0x03;
    buffer1[1] = 0xc9; 
	  buffer1[2] = 0x0c;
    buffer1[3] = 0x11; 	
    SSD2828_W_Array(SigMode,channel,buffer1,0);//VRGH_MTP[7:0] = 0x0F (6.2V)
		delay_ms(20); 
	  buffer1[0] = 0x04;
    buffer1[1] = 0xEB; 
	  buffer1[2] = 0x87;
    buffer1[3] = 0x78; 
	  buffer1[4] = 0x01;		
    SSD2828_W_Array(SigMode,channel,buffer1,0);//Enable Internal MTP Power 
		delay_ms(60); 	
	  buffer1[0] = 0x02;
    buffer1[1] = 0xCA; 
	  buffer1[2] = 0x01;		
    SSD2828_W_Array(SigMode,channel,buffer1,0); //PDSTB = 1
		delay_ms(20); 
		buffer1[0] = 0x03;
    buffer1[1] = 0xCA; 
	  buffer1[2] = 0x01;	
	  buffer1[3] = 0x02;	//PTM_RB[3:0] = 2 (Program Mode)
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(20); 

	
		if(buffer[3]==0x02)  //otp Gamma
		{	 
			  buffer1[0] = 0x02;        
				buffer1[1] = 0xCC;
				buffer1[2] = 0x30;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
				delay_ms(1); 		
				buffer1[0] = 0x02;        
				buffer1[1] = 0xCE;
				buffer1[2] = 0x01;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
				delay_ms(5);			
				SSD2828_W_Array(SigMode,channel,OTP_en_PAGE2,0);	
		}
		else if(buffer[3]==0xA0)  //otp ALL
		{
			SSD2828_W_Array(SigMode,channel,OTP_all_PAGE,0);	
		}
		else if(buffer[3]==0x00)  //otp PAGE 0
		{
 			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE0,0);	
		}
		else if(buffer[3]==0x01)  //otp PAGE 1
		{	 
 			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE1,0);	
		}
		else if(buffer[3]==0x03)  //otp PAGE 5
		{
  		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE3,0);	
		}
		else if(buffer[3]==0x04)  //otp PAGE 4
		{		 
			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE4,0);	
		}
		else if(buffer[3]==0x05)  //otp PAGE 5
		{
  		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE5,0);	
		}
		else if(buffer[3]==0x06)  //otp PAGE 6
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE6,0);	
		}		
		else if(buffer[3]==0x07)  //otp PAGE 7
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE7,0);	
		}		
		else if(buffer[3]==0x08)  //otp PAGE 8
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE8,0);	
		}				
		delay_ms(50); 	 
		SSD2828_W_Array(SigMode,channel,PAGE1,0);//Enable CMD2 Page 1
		delay_ms(2);		
    buffer1[0] = 0x04;
    buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;    
	  buffer1[3] = 0x5A;    
	  buffer1[4] = 0x3C;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);			
		delay_ms(5000);//	x must be large than 3s if need program all page
	//	SSD2828_W_Array(SigMode,channel,OTP_en_off,0);		
//		buffer[1] = 0x0E;
		buffer[2] = 0x02;
		buffer[3] = Uart_Error_None;   //?? OTP Gamma??  :OK
		buffer[4] = 0;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}


void ProcessForIc34( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(buffer[1])
	{
    case 0x01:                                      //????
         NT38360_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
    case 0x02:                                      //????
         NT38360_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;		
    case 0x03:                                      //?Gamma??    
         NT38360_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;
    case 0x04:                                      //?Gamma??
         NT38360_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
    case 0x05:                                      //?Gamma??
         NT38360_Write_Auto_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 		
		case 0x0D://OTP Gamma
         NT38360_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);  break;

    default:	break;	
	}	
}


